﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using Oracle.ManagedDataAccess.Client;

namespace YJ.Data.ORACLE
{
    public class HastenLog : YJ.Data.Interface.IHastenLog
    {
        private DBHelper dbHelper = new DBHelper();
        /// <summary>
        /// 构造函数
        /// </summary>
        public HastenLog()
        {
        }
        /// <summary>
        /// 添加记录
        /// </summary>
        /// <param name="model">YJ.Data.Model.HastenLog实体类</param>
        /// <returns>操作所影响的行数</returns>
        public int Add(YJ.Data.Model.HastenLog model)
        {
            string sql = @"INSERT INTO HastenLog
				(ID,OthersParams,Users,Types,Contents,SendUser,SendUserName,SendTime) 
				VALUES(:ID,:OthersParams,:Users,:Types,:Contents,:SendUser,:SendUserName,:SendTime)";
            OracleParameter[] parameters = new OracleParameter[]{
				new OracleParameter(":ID", OracleDbType.Varchar2){ Value = model.ID },
				new OracleParameter(":OthersParams", OracleDbType.Varchar2){ Value = model.OthersParams },
				new OracleParameter(":Users", OracleDbType.Varchar2){ Value = model.Users },
				new OracleParameter(":Types", OracleDbType.Varchar2){ Value = model.Types },
				new OracleParameter(":Contents", OracleDbType.NVarchar2){ Value = model.Contents },
				new OracleParameter(":SendUser", OracleDbType.Varchar2){ Value = model.SendUser },
				new OracleParameter(":SendUserName", OracleDbType.Varchar2){ Value = model.SendUserName },
				new OracleParameter(":SendTime", OracleDbType.Date){ Value = model.SendTime }
			};
            return dbHelper.Execute(sql, parameters);
        }
        /// <summary>
        /// 更新记录
        /// </summary>
        /// <param name="model">YJ.Data.Model.HastenLog实体类</param>
        public int Update(YJ.Data.Model.HastenLog model)
        {
            string sql = @"UPDATE HastenLog SET 
				OthersParams=:OthersParams,Users=:Users,Types=:Types,Contents=:Contents,SendUser=:SendUser,SendUserName=:SendUserName,SendTime=:SendTime
				WHERE ID=:ID";
            OracleParameter[] parameters = new OracleParameter[]{
				new OracleParameter(":OthersParams", OracleDbType.Varchar2){ Value = model.OthersParams },
				new OracleParameter(":Users", OracleDbType.Varchar2){ Value = model.Users },
				new OracleParameter(":Types", OracleDbType.Varchar2){ Value = model.Types },
				new OracleParameter(":Contents", OracleDbType.NVarchar2){ Value = model.Contents },
				new OracleParameter(":SendUser", OracleDbType.Varchar2){ Value = model.SendUser },
				new OracleParameter(":SendUserName", OracleDbType.Varchar2){ Value = model.SendUserName },
				new OracleParameter(":SendTime", OracleDbType.Date){ Value = model.SendTime },
				new OracleParameter(":ID", OracleDbType.Varchar2){ Value = model.ID }
			};
            return dbHelper.Execute(sql, parameters);
        }
        /// <summary>
        /// 删除记录
        /// </summary>
        public int Delete(Guid id)
        {
            string sql = "DELETE FROM HastenLog WHERE ID=:ID";
            OracleParameter[] parameters = new OracleParameter[]{
				new OracleParameter(":ID", OracleDbType.Varchar2){ Value = id }
			};
            return dbHelper.Execute(sql, parameters);
        }
        /// <summary>
        /// 将DataRedar转换为List
        /// </summary>
        private List<YJ.Data.Model.HastenLog> DataReaderToList(OracleDataReader dataReader)
        {
            List<YJ.Data.Model.HastenLog> List = new List<YJ.Data.Model.HastenLog>();
            YJ.Data.Model.HastenLog model = null;
            while (dataReader.Read())
            {
                model = new YJ.Data.Model.HastenLog();
                model.ID = dataReader.GetGuid(0);
                model.OthersParams = dataReader.GetString(1);
                model.Users = dataReader.GetString(2);
                model.Types = dataReader.GetString(3);
                model.Contents = dataReader.GetString(4);
                model.SendUser = dataReader.GetGuid(5);
                model.SendUserName = dataReader.GetString(6);
                model.SendTime = dataReader.GetDateTime(7);
                List.Add(model);
            }
            return List;
        }
        /// <summary>
        /// 查询所有记录
        /// </summary>
        public List<YJ.Data.Model.HastenLog> GetAll()
        {
            string sql = "SELECT * FROM HastenLog";
            OracleDataReader dataReader = dbHelper.GetDataReader(sql);
            List<YJ.Data.Model.HastenLog> List = DataReaderToList(dataReader);
            dataReader.Close();
            return List;
        }
        /// <summary>
        /// 查询记录数
        /// </summary>
        public long GetCount()
        {
            string sql = "SELECT COUNT(*) FROM HastenLog";
            long count;
            return long.TryParse(dbHelper.GetFieldValue(sql), out count) ? count : 0;
        }
        /// <summary>
        /// 根据主键查询一条记录
        /// </summary>
        public YJ.Data.Model.HastenLog Get(Guid id)
        {
            string sql = "SELECT * FROM HastenLog WHERE ID=:ID";
            OracleParameter[] parameters = new OracleParameter[]{
				new OracleParameter(":ID", OracleDbType.Varchar2){ Value = id }
			};
            OracleDataReader dataReader = dbHelper.GetDataReader(sql, parameters);
            List<YJ.Data.Model.HastenLog> List = DataReaderToList(dataReader);
            dataReader.Close();
            return List.Count > 0 ? List[0] : null;
        }
    }
}